<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use EasyWeChat\Foundation\Application;
use App\User;
use DB;

class WxController extends Controller
{
    public $app = '';
    public function __construct(){
        $options = [
            'debug'  => true,
            'app_id' => 'wx05a1fddf4522707b',
            'secret' => '836c97f4f14b9c83cee3047f1948eee2',
            'token'  => 'hongwei',
            // 'aes_key' => null, // 可选
            'log' => [
                'level' => 'debug',
                'file'  => public_path('/log/easywechat.log'), // XXX: 绝对路径！！！！
            ],
            //...
        ];
        $this->app = new Application($options);
    }
    public function index(){

        // $response = $app->server->serve();
        $server = $this->app->server;
        $server->setMessageHandler(function($msg){
            if($msg->MsgType == 'event'){
                return $this->shijian($msg);
            }else{
                return $this->xiaoxi($msg);
            }

        });
        //返回消息给微信服务器
        $server->serve()->send();
        // 将响应输出
        // return $response;
    }

    //事件处理
    public function shijian($msg){
        $userModel = new User;
        $openId = $msg->FromUserName;
        if($msg->Event == 'subscribe'){
            $user = $userModel->where('openid',$openId)->first();
            if($user){
                $user->sta = 1;
                $user->save();
            }else{
                $userServer = $this->app->user;
                $userInfo = $userServer->get($openId);

                $userModel->name = $userInfo->nickname;
                $userModel->openid = $openId;
                $userModel->subtime = time();
                //如果二维码中有参数
                if($msg->EventKey){
                    $code = $msg->EventKey;
                    $openid = str_replace('qrscene_','',$code);
                    $uId = DB::table('users')->where('openid',$openid)->first();
                    $userModel->p1 = $uId->uid;
                    $userModel->p2 = $uId->p1;
                    $userModel->p3 = $uId->p2;
                }
                $userModel->save();
                $this->erweima($openId);
            }
            return '您好,欢迎进入傻X的公众号';
        }elseif($msg->Event == 'unsubscribe'){

            $user = $userModel->where('openid',$openId)->first();
            if($user){
                $user->sta = 0;
                $user->save();
            }
        }
    }

    //消息处理
    public function xiaoxi($msg){
        if($msg->MsgType == 'text'){
            return '您好,我是宏伟';
        }elseif($msg->MsgType == 'image'){
            return '图片';
        }
    }

    //生成二维码
    public function erweima($openId){
        $qrcode = $this->app->qrcode;
        $result = $qrcode->forever($openId);
        $ticket = $result->ticket;
        $url = $qrcode->url($ticket);

        $content = file_get_contents($url); //获取二维码图片内容
        file_put_contents(public_path('/code/'.$openId.'.jpg'),$content); //写入文件
    }

    public function denglu(){
        return view('welcome');
    }

    public function weibo()
    {
        $weibo_code = $_GET['code'];
        $url = "https://api.weibo.com/oauth2/access_token";
        $data = [
                'client_id' => '1026903030',
                'client_secret' => 'ac7c2c875080ed67c7cc1f2616a7aa0c',
                'grant_type' => 'authorization_code',
                'code' => $weibo_code,
                'redirect_uri' => 'http://fbhongwei.ittun.com/weibo'
        ];
        $atoken = $this->gethttps($url,$data);
        $utoken = json_decode($atoken,true);
        $access_token = $utoken['access_token'];
        $uid = $utoken['uid'];
        $uinfo = file_get_contents("https://api.weibo.com/2/users/show.json?access_token=".$access_token."&uid=".$uid);

        session()->put('uinfo',$uinfo);

    }
}
